<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JsUnit Assertion Tests</title>
<link rel="stylesheet" type="text/css" href="jsunit/css/jsUnitStyle.css">
<script language="JavaScript" type="text/javascript" src="../content/lib/jquery-1.2.6.pack.js"></script>
<script language="JavaScript" type="text/javascript"> imkbizle_jquery = jQuery.noConflict(true); </script>
<script language="JavaScript" type="text/javascript" src="jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" type="text/javascript" src="../content/sourcesite.js"></script>
<script language="JavaScript" type="text/javascript" src="../content/site_adaptors/isteyatirim.js"></script>
<script language="JavaScript" type="text/javascript" src="../content/site_adaptors/akbank.js"></script>
<script language="JavaScript" type="text/javascript" src="../content/site_adaptors/ziraat.js"></script>
<script language="JavaScript" type="text/javascript" src="../content/prefs.js"></script>
<script language="JavaScript" type="text/javascript" src="../content/imkbizle.js"></script>
<script language="JavaScript" type="text/javascript">


// couldn't test some public functions (startup, refreshInformation,
// getIntPref, load) because of unoverwritable private function
// calls. and couldn't test xul related user interface stuff.

function testonSelectStock() {
	var target_button;
	var pref_target;
	var startup_stocks;
	var saved_startup_stocks = "0:ACIBD;1:ALCTL";
	var toggleForGetAttributeCall = 0;
	prefs.getCharPref = function(param_pref_target, default_value) {
		return saved_startup_stocks;
	}
	prefs.setCharPref = function(param_pref_target, param_startup_stocks) {
		pref_target = param_pref_target;
		startup_stocks = param_startup_stocks;	
	}
	var event = {
		target: {
			label: "THYAO",
			parentNode: {
				parentNode: {
					id: "stock-button1",
					h: {},
					getAttribute: function(key) {
						var index = (toggleForGetAttributeCall++) % 2;
						var retvals = ["ALCTL", "THYAO"];
						return retvals[index]; // old stockname
					},
					setAttribute: function(key, value) {
						this.h[key] = value;
					}
				}
			}
		}
	};
	imkbizle.refreshInformation = function(button) {target_button = button};
	imkbizle.onSelectStock(event);
	assertEquals("THYAO", target_button.h["stockname"]);
	assertEquals("prefs.startup_stocks", pref_target);
	assertEquals("0:ACIBD;1:THYAO", startup_stocks);
	saved_startup_stocks = "0:ACIBD;1:ALCTL;2:ARENA";
	imkbizle.onSelectStock(event);
	assertEquals("0:ACIBD;1:THYAO;2:ARENA", startup_stocks);
	saved_startup_stocks = "0:ALCTL";
	event.target.parentNode.parentNode.id = "stock-button0";
	imkbizle.onSelectStock(event);
	assertEquals("0:THYAO", startup_stocks);
}

function testshowSourcePage() {
	var target_url = "";
	var event = {
		target: {
			h: {buttonid: 1, url: "some_url"},
			getAttribute: function(key) {
				return this.h[key];
			}
		}
	};
	document.getElementById = function(key) {return event.target};
	var h = {};
	prefs.getIntPref = function(key) {
		return h[key];
	};
	window.open = function(url_passed) {target_url = url_passed};
	h[prefs.settings.load_location.target] = prefs.NEW_WINDOW;
	imkbizle.showSourcePage(event);
	assertEquals("some_url", target_url);
	target_url = "";
	var browser = {
		loadURI: function(url) {
			target_url = url;
		},
		addTab: function(url) {
			target_url = url;
			return "hede";
		},
		selectedTab: "",
	};
	getBrowser = function() {
		return browser;
	};
	event.target.h["url"] = "some_url2";
	h[prefs.settings.load_location.target] = prefs.UNFOCUSED_TAB
	imkbizle.showSourcePage(event);
	assertEquals("some_url2", target_url);
	assertEquals("", getBrowser().selectedTab);
	event.target.h["url"] = "some_url3";
	h[prefs.settings.load_location.target] = prefs.NEW_TAB
	imkbizle.showSourcePage(event);
	assertEquals("some_url3", target_url);
	assertEquals("hede", getBrowser().selectedTab);

}

function testtoggleRefresh() {
	imkbizle.locale = {
		getString: function(key) {
			var h = {};
			h["label.disable_refresh"] = "disable"
			h["label.enable_refresh"] = "enable"
			return h[key];
		}
	};
	var query = "";
	imkbizle_jquery = function(q) {
		return {
			each: function(f) {
				query = q;
			}
		}
	}
	imkbizle.refresh_enabled = false;
	var event = {target: {label: ""}};
	imkbizle.toggleRefresh(event);
	assertEquals("disable", event.target.label);
	assertEquals("button[class=stock-button]", query);
	query = "";
	imkbizle.refresh_enabled = true;
	imkbizle.toggleRefresh(event);
	assertEquals("enable", event.target.label);
	assertEquals("button[class=stock-button]", query);
}

function testopenPreferences() {
	var url_passed, target_passed, options_passed;
	window.openDialog = function(url, target, options) {
		url_passed = url;
		target_passed = target;
		options_passed = options;
	};
	imkbizle.openPreferences();
	assertEquals("chrome://imkbizle/content/prefs.xul", url_passed);
	assertEquals("_blank", target_passed);
	assertEquals("centerscreen, chrome,resizable=no,dependent=yes", options_passed);
}

function testaddStockButton() {
	var new_div; var first_div;
	imkbizle.locale = {
		getString: function(key) {
			var h = {};
			h["label.remove"] = "remove"
			return h[key];
		}
	};
	var event = {};
	var pref_target;
	var startup_stocks;
	var saved_startup_stocks = "0:ACIBD";
	prefs.getCharPref = function(param_pref_target, default_value) {
		return saved_startup_stocks;
	}
	prefs.setCharPref = function(param_pref_target, param_startup_stocks) {
		pref_target = param_pref_target;
		startup_stocks = param_startup_stocks;	
	}
	var test_setup_for_lastbuttonid = function(lbid) {
		imkbizle_jquery = function() {return [{id: "stock-div" + lbid}]};
		document.getElementById = function(key) {
			if (key == "status-bar") {
				return {
					insertBefore: function(new_div_passed, first_div_passed) {
						new_div = new_div_passed;
						first_div = first_div_passed;
					}
				};
			}
			var div = document.createElement("div");
			div.id = "stock-div" + lbid;
			var menupopup = document.createElement("menupopup");
			menupopup.id = "rightclick-menu" + lbid;
			var showsourcepage_item = document.createElement("menuitem");
			showsourcepage_item.setAttribute("label", "show_source_page");
			showsourcepage_item.setAttribute("buttonid", "stock-button" + lbid);
			showsourcepage_item.setAttribute("onclick", "imkbizle.showSourcePage(event)");
			menupopup.appendChild(showsourcepage_item);
			if (lbid == 0) {
				var disablerefresh_item = document.createElement("menuitem");
				disablerefresh_item.setAttribute("label", "disable_refresh");
				disablerefresh_item.setAttribute("onclick", "imkbizle.toggleRefresh(event)");
				menupopup.appendChild(disablerefresh_item);
			}
			var preferences_item = document.createElement("menuitem");
			preferences_item.setAttribute("label", "preferences");
			preferences_item.setAttribute("onclick", "imkbizle.openPreferences(event)");
			menupopup.appendChild(preferences_item);
			if (lbid > 0) {
				var remove_item = document.createElement("menuitem");
				remove_item.setAttribute("label", "remove");
				remove_item.setAttribute("onclick", "imkbizle.removeStockButton(\'stock-button" + lbid + "\')");
				menupopup.appendChild(remove_item);
			}
			var button =  document.createElement("button");
			button.id = "stock-button" + lbid;
			button.setAttribute("stockname", "THYAO");
			button.setAttribute("type", "menu");
			button.setAttribute("class", "stock-button");
			button.setAttribute("context", "rightclick-menu" + lbid);
			button.setAttribute("oncommand", "imkbizle.onSelectStock(event)");
			button.setAttribute("allowevents", "true");
			var button_menupopup = document.createElement("menupopup");
			button_menupopup.id  = "stock-menu" + lbid;
			button.appendChild(button_menupopup);
			div.appendChild(menupopup);
			div.appendChild(button);
			return div;
		};
	};

	test_setup_for_lastbuttonid(0);
	imkbizle.addStockButton(event);
	assertEquals("stock-div0", first_div.id);
	assertEquals("stock-div1", new_div.id);
	var rightclick_menu = new_div.firstChild;
	assertEquals("rightclick_menu1", rightclick_menu.id);
	assertEquals(3, rightclick_menu.childNodes.length);
	assertEquals("show_source_page", rightclick_menu.firstChild.getAttribute("label"));
	assertEquals("imkbizle.showSourcePage(event)", rightclick_menu.firstChild.getAttribute("onclick"));
	assertEquals("preferences", rightclick_menu.firstChild.nextSibling.getAttribute("label"));
	assertEquals("remove", rightclick_menu.firstChild.nextSibling.nextSibling.getAttribute("label"));
	assertEquals("imkbizle.removeStockButton(\'stock-div1\')", rightclick_menu.firstChild.nextSibling.nextSibling.getAttribute("onclick"));
	var button = rightclick_menu.nextSibling;
	assertEquals("stock-button1", button.id);
	assertEquals("stock-button1", rightclick_menu.firstChild.getAttribute("buttonid"));
	assertEquals("rightclick_menu1", button.getAttribute("context"));
	assertEquals("stock-menu1", button.firstChild.id);
	assertEquals("prefs.startup_stocks", pref_target);
	assertEquals("0:ACIBD;1:THYAO", startup_stocks);

	saved_startup_stocks = "0:ACIBD;1:THYAO";
	test_setup_for_lastbuttonid(1);
	imkbizle.addStockButton(event);
	assertEquals("stock-div1", first_div.id);
	assertEquals("stock-div2", new_div.id);
	var rightclick_menu = new_div.firstChild;
	assertEquals("rightclick_menu2", rightclick_menu.id);
	assertEquals(3, rightclick_menu.childNodes.length);
	assertEquals("show_source_page", rightclick_menu.firstChild.getAttribute("label"));
	assertEquals("imkbizle.showSourcePage(event)", rightclick_menu.firstChild.getAttribute("onclick"));
	assertEquals("preferences", rightclick_menu.firstChild.nextSibling.getAttribute("label"));
	assertEquals("remove", rightclick_menu.firstChild.nextSibling.nextSibling.getAttribute("label"));
	assertEquals("imkbizle.removeStockButton(\'stock-div2\')", rightclick_menu.firstChild.nextSibling.nextSibling.getAttribute("onclick"));
	var button = rightclick_menu.nextSibling;
	assertEquals("stock-button2", button.id);
	assertEquals("stock-button2", rightclick_menu.firstChild.getAttribute("buttonid"));
	assertEquals("rightclick_menu2", button.getAttribute("context"));
	assertEquals("stock-menu2", button.firstChild.id);
	assertEquals("0:ACIBD;1:THYAO;2:THYAO", startup_stocks);
}

function testremoveStockButton() {
	var button_removed;
	var pref_target;
	var startup_stocks;
	var saved_startup_stocks = "0:ACIBD;2:THYAO";
	prefs.getCharPref = function(param_pref_target, default_value) {
		return saved_startup_stocks;
	}
	prefs.setCharPref = function(param_pref_target, param_startup_stocks) {
		pref_target = param_pref_target;
		startup_stocks = param_startup_stocks;	
	}
	var button_exists = {
		lastChild: {
			id: "stock-button2",
			getAttribute: function(key) {
				return "THYAO";	
			}
		},
		parentNode: {
			removeChild: function(button_div) {
				button_removed = button_div;
			}
		}
	};
	document.getElementById = function(key) {return button_exists};
	imkbizle.removeStockButton(2);
	assertEquals(button_exists, button_removed);
	assertEquals("prefs.startup_stocks", pref_target);
	assertEquals("0:ACIBD", startup_stocks);
	saved_startup_stocks = "0:ACIBD;2:THYAO;3:ARENA";
	imkbizle.removeStockButton(2);
	assertEquals("0:ACIBD;3:ARENA", startup_stocks);
}

</script>
</head>

<body>
<h1>IMKBIzle Main Class Tests</h1>

<p>This page contains tests for the main container for imkbizle.</p>
</body>
</html>
